﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using SQLite;
using System.Diagnostics;

namespace DM_Journal
{
    public partial class Form1 : Form
    {
       private SQLite.SQLite sql;
       private bool initialized = false;
       private DataTable dt;
    
        public Form1()
        {
            InitializeComponent();
            About obj = new About();
            obj.ShowDialog();

        }
        private void Init()
        {
            string a = "data source=" + System.Environment.UserName.Trim() + ".sqlite";
            SQLite.SQLite.SQLiteCreateDB(System.Environment.UserName.Trim() + ".sqlite");
            SQLite.SQLite con = new SQLite.SQLite(a);
            
            con.SQLiteCreateTable("CREATE TABLE DM(Id integer PRIMARY KEY AUTOINCREMENT, Note TEXT,Date TEXT,ImageofTheDay TEXT);");
            
            con.SQLiteCloseConnection();
        }
        private void SQLiteShow(DataTable dT)
        {
            if (dT.Rows.Count > 0)
            {
                rtxtNotes.Text = dT.Rows[0].Field<string>("Note");
                pbImage.Image=Base64ToImage(dT.Rows[0].Field<string>("ImageofTheDay"));
                
            }
        }


        public string ImageToBase64(Image image,
          System.Drawing.Imaging.ImageFormat format)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                // Convert Image to byte[]
                image.Save(ms, format);
                byte[] imageBytes = ms.ToArray();

                // Convert byte[] to Base64 String
                string base64String = Convert.ToBase64String(imageBytes);
                return base64String;
            }
        }



        public Image Base64ToImage(string base64String)
        {
            // Convert Base64 String to byte[]
            byte[] imageBytes = Convert.FromBase64String(base64String);
            using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
            {

                // Convert byte[] to Image
                ms.Write(imageBytes, 0, imageBytes.Length);
                Image image = Image.FromStream(ms, true);
                return image;
            }
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            if (initialized == false&&!File.Exists(System.Environment.UserName.Trim() + ".sqlite"))
            {
                Init();
                initialized = true;
            }


            string a = "data source=" + System.Environment.UserName.Trim() + ".sqlite";
            sql = new SQLite.SQLite(a);
            dtpNotes.Value = DateTime.Now.Date;
            dt = new DataTable();
            dt=sql.SQLiteSelect(string.Format("SELECT Note,ImageofTheDay FROM DM WHERE Date='{0}'; ", dtpNotes.Value.Date.ToString()));

          
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (rtxtNotes.Text.Trim()!= "")
            {
                Dictionary<string, object> dic = new Dictionary<string, object>();
                dic.Add("Note", rtxtNotes.Text);
                dic.Add("Date", dtpNotes.Value.ToString());
                DataTable dT = new DataTable();
                dT=sql.SQLiteSelect(string.Format("SELECT Note FROM DM WHERE Date='{0}'; ", dtpNotes.Value.Date.ToString()));
                if (pbImage.Image != null)
                {
                    dic.Add("ImageofTheDay", ImageToBase64(pbImage.Image,System.Drawing.Imaging.ImageFormat.Jpeg));
                }
                if (dT.Rows.Count == 0)
                {
                    sql.SQLiteInsert(dic, "DM");
                    
                }
                else
                {
                    Dictionary<string,string> terms=new Dictionary<string,string>();
                    terms.Add("Date",dtpNotes.Value.Date.ToString());
                    sql.SQLiteUpdate(dic, terms, "DM");
                }
                
                dt = sql.SQLiteSelect(string.Format("SELECT Note,ImageofTheDay FROM DM WHERE Date='{0}'; ", dtpNotes.Value.Date.ToString()));
                SQLiteShow(dt);


                
            }
        }

        private void dtpNotes_ValueChanged(object sender, EventArgs e)
        {
            rtxtNotes.Text = "";
            pbImage.Image = null;
            dt = sql.SQLiteSelect(string.Format("SELECT Note,ImageofTheDay FROM DM Where Date='{0}';",dtpNotes.Value.Date.ToString()));
            SQLiteShow(dt);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            Dictionary<string,string> dic=new Dictionary<string,string>();
            dic.Add("Date",dtpNotes.Value.Date.ToString());
            dic.Add("Note",rtxtNotes.Text);
            sql.SQLiteDelete(dic, "DM");
            rtxtNotes.Text = "";
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void deleteAllNotesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            sql.SQLiteDelete(new Dictionary<string,string>(),"DM");
           rtxtNotes.Text="";
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About obj = new About();
            obj.ShowDialog();
        }

        private void websiteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("https://dmarm.wordpress.com");
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "Image Files (*.bmp, *.jpg,*.png)|*.bmp;*.jpg;*.png";
            DialogResult res = dialog.ShowDialog();
            if (res == DialogResult.OK)
            {
                txtLocation.Text = dialog.FileName;
                pbImage.Image = Image.FromFile(@txtLocation.Text);
                pbImage.SizeMode = PictureBoxSizeMode.StretchImage;
                
            }
        }
    }
}
